import { Component, OnInit } from '@angular/core';
import { HttpService } from '../http.service';
import * as ons from 'onsenui';
import { Router } from '@angular/router';


@Component({
  selector: 'app-bind-card',
  templateUrl: './bind-card.component.html',
  styleUrls: ['./bind-card.component.less']
})
export class BindCardComponent implements OnInit {
  cardNo: string = '';
  name: string = '';
  bank: string = '';
  uploading:Boolean = false;

  beforeSave(): Boolean {
    if(!(this.cardNo && this.name && this.bank)) {
      ons.notification.toast('表格信息需填写完整!', {timeout: 2000});
      return false;
    }
    return true;
  }

  async save() {
    if(!this.beforeSave()) return;
    if(this.uploading) return;
    this.uploading = true;
    const {success, msg} = await this.http.post({
      name: 'car.bind.bank.card',
      bankcard_num: this.cardNo,
      owner_name: this.name,
      bank: this.bank
    })
    if(success) {
      this.route.navigate(['editCard']);
    }
    else {
      this.uploading = false;
      ons.notification.toast(msg, {timeout: 2000});
    }
  }

  constructor(private route: Router, private http: HttpService) {

  }

  ngOnInit() {
  }

}
